package com.pricl.biz.upload.manager;

import java.io.File;
import java.util.Date;
import java.util.Set;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.pricl.biz.file.constant.FileStatusConstant;
import com.pricl.biz.file.entity.FileInfo;
import com.pricl.frame.common.utils.Utils;
import com.pricl.frame.core.constants.FileManagerConstants;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.MultimediaInfo;


/**
 * <ol>
 * <li>音频上传管理类</li>
 * <ol>
 * 
 * @author jackzhong
 * @since 2014年10月31日下午9:25:22
 */
@Component("audioUploadFileManager")
public class AudioUploadFileManager extends AbstractUploadFileManager {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.CDT.modules.supper.manager.upload.UploadFileManager#
	 * getAllowFileExtensions()
	 */
	@Override
	public Set<String> getAllowFileExtensions() {
		return getAllowFileExtensions(FileManagerConstants.UPLOAD_AUDIO_ALLOW_FORMAT);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.CDT.modules.supper.manager.upload.UploadFileManager#getAllowFileSize
	 * ()
	 */
	@Override
	public long getAllowFileSize() {
		// TODO Auto-generated method stub
		return getAllowFileSize(FileManagerConstants.UPLOAD_AUDIO_ALLOW_SIZE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.CDT.modules.supper.manager.upload.UploadFileManager#
	 * getFileUploadRelativePath()
	 */
	@Override
	public String getFileUploadRelativePath() {
		// TODO Auto-generated method stub
		return getAudioUploadRelativePath();
	}

	//获取视频音频的时间长度
	public long getDuration(CommonsMultipartFile cf){
		long duration = 0l;
		Encoder encoder = new Encoder();
        DiskFileItem fi = null;
        File source = null;
		MultimediaInfo m = null;
		try {
			fi = (DiskFileItem)cf.getFileItem();
			source = fi.getStoreLocation();
			m = encoder.getInfo(source);
			duration = m.getDuration();
			return duration;
		} catch (InputFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return duration;
		} catch (EncoderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return duration;
		}
	}
	@Override
	public FileInfo createFileInfo(MultipartFile file){
		CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile)file;
		FileInfo fileInfo = new FileInfo();
		fileInfo.setOname(file.getOriginalFilename());
		fileInfo.setContentType(file.getContentType());
		fileInfo.setLength(file.getSize());
		if (fileInfo.getId() == null || fileInfo.getId().isEmpty()) {
			fileInfo.setId(Utils.uuid());
		}
		String[] names = formatName(fileInfo.getOname());
		if (fileInfo.getName() == null || fileInfo.getName().isEmpty()) {
			fileInfo.setName(names[0]);
		}
		fileInfo.setExtName(names[1]);
		
		// 构建上传路径
		String storageDirectory = getFileUploadDirectory();
		String relativePath = getFileUploadRelativePath();
		StringBuffer fileUploadFullDirectorySb = new StringBuffer();
		fileUploadFullDirectorySb.append(storageDirectory).append(relativePath).append(FileManagerConstants.FILE_DECOLLATOR);
		String fileUploadFullDirectory = fileUploadFullDirectorySb.toString();
		fileInfo.setPath(fileUploadFullDirectory);
		long duration = getDuration(commonsMultipartFile);
		fileInfo.setDuration(duration);
		// 创建日期
		fileInfo.setCreatedDt(new Date());
		// 状态
		fileInfo.setStatus(FileStatusConstant.normal);
		return fileInfo;
	}
}
